package com.wushijia.test;

public class UnsignedByte {

  public short getValue(byte i) {          //将byte转为无符号的数字
    return (short) (i & 0xff);
  }

  public byte toUnsignedByte(short i) {     //将short转为无符号byte
    return (byte) (i & 0xff);
  }

  public static void main(String[] args) {
    UnsignedByte ins = new UnsignedByte();
    short[] shorts = new short[256];            //声明一个short数组
    for (int i = 0; i < shorts.length; i++) {   //数值不能超过无符号byte的上限
      shorts[i] = (short) i;
    }
    byte[] bytes = new byte[256];               //使用byte数组替代short数组
    for (int i = 0; i < bytes.length; i++) {    //将short数组的数据存到byte数组中
      bytes[i] = ins.toUnsignedByte(shorts[i]);
    }
    for (byte aByte : bytes) {    //从byte数组中取出无符号的byte
      System.out.println(aByte);
      System.out.println(ins.getValue(aByte) + " ");
    }
  }
}
